package com.atguigu.lease.common.converter;

import com.atguigu.lease.model.enums.ItemType;
import org.springframework.core.convert.converter.Converter;
import org.springframework.stereotype.Component;

//@Component
public class StringToItemTypeConverter implements Converter<String, ItemType> {

    @Override
    public ItemType convert(String source) {
        //1.获取枚举中的所有类型
        ItemType[] values = ItemType.values();
        //2.遍历所有的类型
        for (ItemType itemType : values) {
            //3.当前遍历的类型的code值和传进来的值相等
            if(itemType.getCode().equals(Integer.valueOf(source))){
                return itemType;
            }
        }
        //错误的数据，抛异常
        throw new IllegalArgumentException("非法的参数");
    }
}
